/**
 * Created by Administrator on 2018/10/18.
 */

// 格式化状态
function statusFormatter(value) {
    var spanHtml = '';
    switch (value) {
        case '已提交':
            spanHtml = '<span class="label label-primary">已提交</span>';
            break;
        case '已完成':
            spanHtml = '<span class="label label-success">已完成</span>';
            break;
        case '已驳回':
            spanHtml = '<span class="label label-danger">已驳回</span>';
            break;
        case '进行中':
            spanHtml = '<span class="label label-info">进行中</span>';
            break;
        case '未开始':
            spanHtml = '<span class="label label-warning">未开始</span>';
            break;
        case '已超期':
            spanHtml = '<span class="label label-danger">已超期</span>';
            break;
    }
    // console.log(spanHtml);
    return spanHtml;
}

$(function () {
    var user = getUser();
    var roleIdListString = user.roleIdList.join(',');
    roleIdListString += ',0101';
    console.log(roleIdListString);
    $("#jqGrid").jqGrid({
        url: baseURL + 'generator/projectentity/list/' + user.userId + '/' + roleIdListString.split(','),
        datatype: "json",
        colModel: [
            {label: '项目编号', name: 'projectNumber', index: 'project_number', width: 80, sortable: true},
            {label: '项目名称', name: 'projectName', index: 'project_name', width: 80, sortable: false},
            {label: '项目状态', name: 'projectStatus', index: 'project_status', width: 80, sortable: false},
            {label: '当前任务', name: 'projectTask', index: 'projectTask', width: 80, sortable: false},
            {
                label: '任务状态', name: 'projectTaskStatus', index: 'projectTaskStatus', width: 80, sortable: false,
                formatter: function (value) {
                    return statusFormatter(value);
                }
            },
            {
                label: '电压类型',
                name: 'voltageType',
                index: 'voltage_type',
                width: 80,
                sortable: false,
                formatter: function (value, options, row) {
                    console.log(value);
                    if (value == "1") {
                        return '<span >' + '中压' + '</span>';
                    } else {
                        return '<span >' + '高压' + '</span>';
                    }
                }
            },
            {label: '项目经理', name: 'projectManager', index: 'project_manager', width: 80, sortable: false},
            {label: '申报时间', name: 'projectTime', index: 'project_time', width: 80, sortable: true},
            {label: '开始时间', name: 'startTime', index: 'start_time', width: 80, sortable: false},
            {label: '结束时间', name: 'endTime', index: 'end_time', width: 80, sortable: false},
            {label: '支付比例', name: 'paymentRatio', index: 'paymentRatio', width: 80, sortable: false},
            {
                label: '操作', name: 'endTime', width: 80, sortable: false, formatter: function (value, options, row) {
                    console.log(row);
                    return '<a class="btn-details" href="javascript:void(0);" data-id="' + row.id + '">' + '详情' + '</a>';
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: false,
        multiSort: false,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            $('#jqGrid').on('click', '.btn-details', function (e) {
                var projectId = $(this).attr('data-id');
                var pWindow = window.parent;
                var oIframe = $('#main-content iframe', pWindow.document);
                // 改变首页的 indexVm 的 data，触发 updated
                pWindow.indexVm.main = 'modules/project/projectdetails.html?projectId=' + projectId;
                pWindow.location.hash = 'modules/project/projectdetails.html?projectId=' + projectId;
                oIframe.attr('src', 'modules/project/projectdetails.html?projectId=' + projectId);
            })
        }
    });

    // 时间控件
    $('#projectTimeStart').datetimepicker({
        language:'zh-CN',
        format: 'yyyy-mm-dd',
        todayHighlight:1,
        showMeridian: 0,
        autoclose: 1,
        minView: 2
    });
    // 时间控件
    $('#projectTimeEnd').datetimepicker({
        language:'zh-CN',
        format: 'yyyy-mm-dd',
        todayHighlight:1,
        showMeridian: 0,
        autoclose: 1,
        minView: 2
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        projectEntity: {},
        searchOptions: {
            projectStatus: '11',
            projectName: '',
            projectManager: '',
            projectNumber: '',
            voltageType: '',
            projectType:'',
            projectLevel:'',
            projectApplicant:'',
            projectArea:'',
            projectTimeStart:'',
            projectTimeEnd:'',
            paymentRatioStart:20,
            paymentRatioEnd:70
        },
        projectManagerList: [],  //所有人员
        projectApplicantList: [],   //项目申请方列表
        projectAreaList: []  //项目所属区域
    },
    mounted: function () {
        /* 选择项目经理 */
        var _this = this;
        $.get(baseURL + 'sys/user/queryAll', function (result) {
            _this.projectManagerList = result.userList;
            _this.projectManagerList.unshift({id: "0", text: "全部"});
            _this.searchOptions.projectManager = "0";
        });

        /* 选择项目申请方 */
        $.get(baseURL + 'generator/projectapplicantinfo/alllist', function (result) {
            _this.projectApplicantList = result.applicantList;
            _this.projectApplicantList.unshift({id: "0", text: "全部"});
            _this.searchOptions.projectApplicant = "0";
        });

        /* 选择项目所属区域 */
        _this.projectAreaList = [
            {
                id: 0,
                text: '全部'
            },
            {
                id: 10001,
                text: '武昌区'
            },
            {
                id: 10002,
                text: '汉口区'
            },
            {
                id: 10003,
                text: '汉阳区'
            },
            {
                id: 10004,
                text: '东新区'
            },
            {
                id: 10005,
                text: '沌口区'
            },
            {
                id: 10006,
                text: '东西湖区'
            },
            {
                id: 10007,
                text: '黄陂区'
            },
            {
                id: 10008,
                text: '新洲区'
            },
            {
                id: 10009,
                text: '汉南区'
            },
            {
                id: 10010,
                text: '江夏区'
            },
            {
                id: 10011,
                text: '蔡甸区'
            }
        ];

        $('#ratio-inp').slider();
        $('#ratio-inp').on('change', function (oRatio) {
            _this.searchOptions.paymentRatioStart = oRatio.value.newValue[0];
            _this.searchOptions.paymentRatioEnd = oRatio.value.newValue[1];
        });
        _this.searchOptions.voltageType = "0";
        _this.searchOptions.projectType = "0";
        _this.searchOptions.projectLevel = "0";
        _this.searchOptions.projectArea = "0";
    },
    methods: {
        query: function () {
            vm.reload();
        },
        getInfo: function (id) {
            $.get(baseURL + "generator/projectentity/info/" + id, function (r) {
                vm.projectEntity = r.projectEntity;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            // console.log(page);
            console.log(vm.searchOptions);
            // console.log(vm.projectEntity);
            var searchOptions = vm.searchOptions;
            vm.searchOptions.projectTimeStart = $("#projectTimeStart").val();
            vm.searchOptions.projectTimeEnd = $("#projectTimeEnd").val();
            $("#jqGrid").jqGrid("setGridParam", {postData: searchOptions});
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'projectTimeStart': vm.searchOptions.projectTimeStart,
                    'projectTimeEnd': vm.searchOptions.projectTimeEnd
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});
